######################################################
#The Populist Radical Right and Military Intervention#
######################################################

#Author: Tim Haesebrouck


# Clear working space
rm(list = ls())


# Load packages
library(cna)
library(frscore)

#set working directory
setwd(" ")


#upload data and produce configurations table

dat <- read.csv("PRR.csv", row.names = 1, sep = ";",dec = ",")
ct<-configTable(dat, type=c("mv"),verbose =TRUE)
print(ct,show.cases=TRUE)


####SUPPORT[1]####

##Perform Fit Robustness Procedure Coincidence Analysis#
##(Results are presented in table 1 of the article and table A4.1 of the online appendix)

frscored_cna(
   dat,
    fit.range = c(1, 0.75),
    granularity = 0.05,
    output = c("asf"),
    scoretype = c("full", "supermodel", "submodel"),
    normalize = c("none"),
    verbose = FALSE,
    maxsols = 50,
    test.model = NULL,
    print.all = TRUE,outcome="SUPPORT=1")


##check covered cases and consistency/coverage#
##(information presented in table 1 of the article)

##Short solution (without conditions between brackets; consistency and coverage values not between brackets)

#Consistency and raw coverage solution
#Covered cases have 1 in column GOV=2+TERROR=1*ANTEL...   and 1 in Column SUPPORT=1  
#Not covered cases have 0 in column GOV=2+TERROR=1*ANTEL...  and 1 in column SUPPORT = 1
sol1 <- condition("GOV=2+TERROR=1*ANTEL=0+SIZE=0+ANTEL=0*GOV=1<->SUPPORT=1",ct, show.cases=T)
print(sol1, digits = 3,  show.cases=TRUE)

#Consistency and raw coverage path 1 (GOV[2]) 
#Covered cases have 1 in column GOV=2  and 1 in Column SUPPORT=1  
#Deviant cases have 1 in column GOV=2 and 0 in column SUPPORT = 1
gov2 <- condition("GOV=2 -> SUPPORT=1",ct, show.cases=T)
print(gov2, digits = 3,  show.cases=TRUE)

#Unique coverage path 1 (GOV[2])
#Step 1 Calculate coverage solution without GOV[2]
#Step 2 Subtract coverage solution without GOV[2] from coverage solution
condition("TERROR=1*ANTEL=0+SIZE=0+ANTEL=0*GOV=1<->SUPPORT=1",ct, show.cases=T)
round((0.826-0.630), digits = 2)


#Consistency and raw coverage path 2 (SIZE[0]) 
#Covered cases have 1 in column SIZE=0  and 1 in Column SUPPORT=1  
#Deviant cases have 1 in column SIZE=0and 0 in column SUPPORT = 1
S0 <- condition("SIZE=0 -> SUPPORT=1",ct, show.cases=T)
print(S0, digits = 3,  show.cases=TRUE)

#Unique coverage path 2 (SIZE[0]) 
#Step 1 Calculate coverage solution without SIZE[0]
#Step 2 Subtract coverage solution without SIZE[0] from coverage solution
condition("GOV=2+TERROR=1*ANTEL=0+ANTEL=0*GOV=1<->SUPPORT=1",ct, show.cases=T)
round((0.826-0.630), digits = 2)

#Consistency and raw coverage path 3 (ANTEL[0]*GOV[1]) 
#Covered cases have 1 in column ANTEL=0*GOV=1  and 1 in Column SUPPORT=1  
#Deviant cases have 1 in column ANTEL=0*GOV=1 and 0 in column SUPPORT = 1
A0G1 <- condition("ANTEL=0*GOV=1-> SUPPORT=1",ct, show.cases=T)
print(A0G1, digits = 3,  show.cases=TRUE)

#Unique coverage  path 3 (ANTEL[0]*GOV[1]) 
#Step 1 Calculate coverage solution without ANTEL[0]*GOV[1]
#Step 2 Subtract coverage solution without ANTEL[0]*GOV[1] from coverage solution
condition("GOV=2+TERROR=1*ANTEL=0+SIZE=0<->SUPPORT=1",ct, show.cases=T)
round((0.826-0.587), digits = 2)


#Consistency and raw coverage path 4 (ANTEL[0]*TERROR[1]) 
#Covered cases have 1 in column TERROR=1*ANTEL=0 and 1 in Column SUPPORT=1  
#Deviant cases have 1 in column TERROR=1*ANTEL=0 and 0 in column SUPPORT = 1
T1A0 <- condition("TERROR=1*ANTEL=0  -> SUPPORT=1",ct, show.cases=T)
print(T1A0, digits = 3,  show.cases=TRUE)

#Unique coverage  path 4 (ANTEL[0]*TERROR[1]) 
#Step 1 Calculate coverage solution without ANTEL[0]*TERROR[1]
#Step 2 Subtract coverage solution without ANTEL[0]*TERROR[1] from coverage solution
condition("GOV=2+SIZE=0+ANTEL=0*GOV=1<->SUPPORT=1",ct, show.cases=T)
round((0.826-0.761), digits = 2)

##Long solution (with conditions between brackets; consistency and coverage values between brackets)

#Consistency and raw coverage solution
#Covered cases have 1 in column GOV=2+TERROR=1*ANTEL...   and 1 in Column SUPPORT=1  
#Not covered cases have 0 in column GOV=2+TERROR=1*ANTEL...  and 1 in column SUPPORT = 1
sol2 <- condition("GOV=2+TERROR=1*ANTEL=0+SIZE=0*EXP=0+EXP=0*ANTEL=0*GOV=1<->SUPPORT=1",ct, show.cases=T)
print(sol2, digits = 3,  show.cases=TRUE)


#Consistency and raw coverage path 1 (GOV[2]) 
#Covered cases have 1 in column GOV=2  and 1 in Column SUPPORT=1  
#Deviant cases have 1 in column GOV=2 and 0 in column SUPPORT = 1
gov2 <- condition("GOV=2 -> SUPPORT=1",ct, show.cases=T)
print(gov2, digits = 3,  show.cases=TRUE)

#Unique coverage path 1 (GOV[2])
#Step 1 Calculate coverage solution without GOV[2]
#Step 2 Subtract coverage solution without GOV[2] from coverage solution
condition("TERROR=1*ANTEL=0+SIZE=0*EXP=0+EXP=0*ANTEL=0*GOV=1<->SUPPORT=1",ct, show.cases=T)
round((0.783-0.543), digits = 2)


#Consistency and raw coverage path 2 (SIZE[0]*EXP[0]) 
#Covered cases have 1 in column SIZE=0*EXP=0  and 1 in Column SUPPORT=1  
#Deviant cases have 1 in column SIZE=0*EXP=0 and 0 in column SUPPORT = 1
S0E0 <- condition("SIZE=0*EXP=0 -> SUPPORT=1",ct, show.cases=T)
print(S0E0, digits = 3,  show.cases=TRUE)

#Unique coverage path 2 (SIZE[0]*EXP[0]) 
#Step 1 Calculate coverage solution without SIZE[0]
#Step 2 Subtract coverage solution without SIZE[0] from coverage solution
condition("GOV=2+TERROR=1*ANTEL=0+EXP=0*ANTEL=0*GOV=1<->SUPPORT=1",ct, show.cases=T)
round((0.783-0.587), digits = 2)

#Consistency and raw coverage path 3 (ANTEL[0]*GOV[1]](*EXP[0]) 
#Covered cases have 1 in column ANTEL=0*GOV=1*EXP=0  and 1 in Column SUPPORT=1  
#Deviant cases have 1 in column ANTEL=0*GOV=1*EXP=0 and 0 in column SUPPORT = 1
A0G1E0 <- condition("ANTEL=0*GOV=1*EXP=0-> SUPPORT=1",ct, show.cases=T)
print(A0G1E0, digits = 3,  show.cases=TRUE)

#Unique coverage  path 3 (ANTEL[0]*GOV[1]](*EXP[0]) 
#Step 1 Calculate coverage solution without ANTEL[0]*GOV[1](*EXP[0]) 
#Step 2 Subtract coverage solution without ANTEL[0]*GOV[1](*EXP[0])  from coverage solution
condition("GOV=2+TERROR=1*ANTEL=0+SIZE=0*EXP=0<->SUPPORT=1",ct, show.cases=T)
round((0.783-0.587), digits = 2)


#Consistency and raw coverage path 4 (ANTEL[0]*TERROR[1]) 
#Covered cases have 1 in column TERROR=1*ANTEL=0 and 1 in Column SUPPORT=1  
#Deviant cases have 1 in column TERROR=1*ANTEL=0 and 0 in column SUPPORT = 1
T1A0 <- condition("TERROR=1*ANTEL=0  -> SUPPORT=1",ct, show.cases=T)
print(T1A0, digits = 3,  show.cases=TRUE)

#Unique coverage  path 4 (ANTEL[0]*TERROR[1]) 
#Step 1 Calculate coverage solution without ANTEL[0]*TERROR[1]
#Step 2 Subtract coverage solution without ANTEL[0]*TERROR[1] from coverage solution
condition("GOV=2+SIZE=0*EXP=0+EXP=0*ANTEL=0*GOV=1<->SUPPORT=1",ct, show.cases=T)
round((0.783-0.717), digits = 2)

####SUPPORT[0]####
##Perform Fit Robustness Procedure Coincidence Analysis#
##(Results are presented in table 2 of the article and table A4.2 of the online appendix)

frscored_cna(
    dat,
    fit.range = c(1, 0.75),
    granularity = 0.05,
    output = c("asf"),
    scoretype = c("full", "supermodel", "submodel"),
    normalize = c( "none"),
    verbose = FALSE,
    maxsols = 50,
    test.model = NULL,
    print.all = FALSE,outcome="SUPPORT=0")

##check covered cases and consistency/coverage#
##(information presented in table 2 of the article)

#Consistency and raw coverage solution
#Covered cases have 1 in column EXP=1*GOV=0+TERROR=0... and 1 in column SUPPORT=0  
#Not covered cases have 0 in column EXP=1*GOV=0+TERROR=0... and 1 in column SUPPORT = 0
sol0 <- condition("EXP=1*GOV=0+TERROR=0*SIZE=1*GOV=0+SIZE=1*ANTEL=1*GOV=1 <-> SUPPORT=0",ct, show.cases=T)
print(sol0, digits = 3,  show.cases=TRUE)

#Consistency and raw coverage path 1 (SIZE[1]*ANTEL[1]*GOV[1]) 
#Covered cases have 1 in column SIZE=1*ANTEL=1*GOV=1   and 1 in column SUPPORT=0  
#Deviant cases have 1 in column SIZE=1*ANTEL=1*GOV=1 and 0 in column SUPPORT = 0
S1A1G1 <- condition("SIZE=1*ANTEL=1*GOV=1 -> SUPPORT=0",ct, show.cases=T)
print(S1A1G1, digits = 3,  show.cases=TRUE)

#Unique coverage path 1 (SIZE[1]*ANTEL[1]*GOV[1]) 
#Step 1 Calculate coverage solution without (SIZE[1]*ANTEL[1]*GOV[1]) 
#Step 2 Subtract coverage solution without (SIZE[1]*ANTEL[1]*GOV[1])  from coverage solution
condition("EXP=1*GOV=0+TERROR=0*SIZE=1*GOV=0 <-> SUPPORT=0",ct, show.cases=T)
round((0.781-0.406), digits = 2)

#Consistency and raw coverage path 2 (SIZE[1]*TERROR[0]*GOV[0]) 
#Covered cases have 1 in column  TERROR=0*SIZE=1*GOV=0 and 1 in column SUPPORT=0  
#Deviant cases have 1 in column TERROR=0*SIZE=1*GOV=0 and 0 in column SUPPORT = 0
T0G0S1 <- condition("TERROR=0*SIZE=1*GOV=0 -> SUPPORT=0",ct, show.cases=T)
print(T0G0S1, digits = 3,  show.cases=TRUE)

#Unique coverage  path 2 (SIZE[1]*TERROR[0]*GOV[0]) 
#Step 1 Calculate coverage solution without (SIZE[1]*TERROR[0]*GOV[0]) 
#Step 2 Subtract coverage solution without (SIZE[1]*TERROR[0]*GOV[0])  from coverage solution
condition("EXP=1*GOV=0+SIZE=1*ANTEL=1*GOV=1 <-> SUPPORT=0",ct, show.cases=T)
round((0.781-0.531), digits = 2)

#Consistency and raw coverage path 3 (EXP[1]*GOV[0]) 
#Covered cases have 1 in column  EXP=1*GOV=0 and 1 in column SUPPORT=0  
#Deviant cases have 1 in columnEXP=1*GOV=0 and 0 in column SUPPORT = 0
E1G0 <- condition("EXP=1*GOV=0 -> SUPPORT=0",ct, show.cases=T)
print(E1G0, digits = 3,  show.cases=TRUE)

#Unique coverage  path 3 (EXP[1]*GOV[0])
#Step 1 Calculate coverage solution without (EXP[1]*GOV[0])
#Step 2 Subtract coverage solution without (EXP[1]*GOV[0])  from coverage solution
condition("TERROR=0*SIZE=1*GOV=0+SIZE=1*ANTEL=1*GOV=1 <-> SUPPORT=0",ct, show.cases=T)
round((0.781-0.719), digits = 2)

